//
//  ExtAVPlayerItem.swift
//  koolcar
//
//  Created by huangweiqiang on 2022/6/20.
//

import UIKit
import AVFoundation

var player_item_status_key: UnsafePointer<String>? = nil
extension AVPlayerItem {
    func playerItemStatusBlock(aBlock: @escaping (AVPlayerItemStatus)->Void) {
        let tempBlock = objc_getAssociatedObject(self, &player_item_status_key) as? (AVPlayerItemStatus)->Void
        if tempBlock == nil {
            objc_setAssociatedObject(self, &player_item_status_key, aBlock, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            self.addObserver(self, forKeyPath: "status", options: .new, context: nil)
        }
    }
    
    ///监听视频播放是否加载完成，然后跳转
    open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "status" {
            let aBlock = objc_getAssociatedObject(self, &player_item_status_key) as? (AVPlayerItemStatus)->Void
            aBlock?(self.status)
        }
    }
    
}
